//
//  ATAreaPicker.m
//  NoPi
//
//  Created by 敖然 on 15/11/17.
//  Copyright © 2015年 nopi.com. All rights reserved.
//

#import "ATAreaPicker.h"
#import <Masonry/Masonry.h>

@interface ATAreaPicker () <UIPickerViewDelegate, UIPickerViewDataSource>
@property (nonatomic, strong) UIPickerView *picker;
@property (nonatomic, strong) NSArray<ATStateResouce *> *states;
@property (nonatomic, strong) ATAreaModel *cacheModel;
@end

@implementation ATAreaPicker
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        [self setup];
    }
    return self;
}
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setup];
    }
    return self;
}
- (void)setup {
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(([UIScreen mainScreen].bounds.size.width))).priorityLow();
        make.height.equalTo(@(217)).priorityMedium();
    }];
    [self addSubview:self.picker];
    [self.picker mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.picker.superview);
    }];
    
    self.states = [ATStateResouce modelArrayWithJson:[NSArray arrayWithPlistData:[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"area.plist" ofType:nil]]]];
    [self pickerView:self.picker didSelectRow:0 inComponent:0];
}
- (void)setType:(ATAreaPickerType)type {
    _type = type;
    [self.picker reloadAllComponents];
}
- (void)setArea:(ATAreaModel *)areaModel {
    [self setArea:areaModel animated:NO];
}
- (void)setArea:(ATAreaModel *)areaModel animated:(BOOL)animated {
    _areaModel = areaModel;
}

#pragma mark - delegate
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return (self.type == ATAreaPickerTypeStateAndCityAndDistrict) ? 3 : 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    switch (component) {
        case 0:
            return self.states.count;
        case 1: {
            NSUInteger index = [pickerView selectedRowInComponent:0];
            if (index < self.states.count) {
                return self.states[index].cities.count;
            }
            break;
        }
        case 2: {
            NSUInteger indexState = [pickerView selectedRowInComponent:0];
            NSUInteger indexCity = [pickerView selectedRowInComponent:1];
            if (indexState < self.states.count) {
                if (indexCity < self.states[indexState].cities.count) {
                    return self.states[indexState].cities[indexCity].areas.count;
                }
            }
            break;
        }
        default:
            break;
    }
    return 0;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    switch (component) {
        case 0: {
            if (row < self.states.count) {
                return self.states[row].state;
            }
            break;
        }
        case 1: {
            NSUInteger index = [pickerView selectedRowInComponent:0];
            if (index < self.states.count) {
                if (row < self.states[index].cities.count) {
                    return self.states[index].cities[row].city;
                }
            }
            break;
        }
        case 2: {
            NSUInteger indexState = [pickerView selectedRowInComponent:0];
            NSUInteger indexCity = [pickerView selectedRowInComponent:1];
            if (indexState < self.states.count) {
                if (indexState < self.states[indexState].cities.count) {
                    if (row < self.states[indexState].cities[indexCity].areas.count) {
                        return self.states[indexState].cities[indexCity].areas[row];
                    }
                }
            }
            break;
        }
        default:
            break;
    }
    return nil;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    switch (component) {
        case 0: {
            [pickerView reloadComponent:1];
            [pickerView selectRow:0 inComponent:1 animated:YES];
            if (pickerView.numberOfComponents >= 3) {
                [pickerView reloadComponent:2];
                [pickerView selectRow:0 inComponent:2 animated:YES];
            }
            break;
        }
        case 1:
            if (pickerView.numberOfComponents >= 3) {
                [pickerView reloadComponent:2];
                [pickerView selectRow:0 inComponent:2 animated:YES];
            }
            break;
        case 2:
            break;
        default:
            break;
    }

    NSUInteger indexState = [pickerView selectedRowInComponent:0];
    self.cacheModel.state = nil;
    self.cacheModel.city = nil;
    self.cacheModel.district = nil;
    if (indexState < self.states.count) {
        self.cacheModel.state = self.states[indexState].state;
        NSUInteger indexCity = [pickerView selectedRowInComponent:1];
        if (indexCity < self.states[indexState].cities.count) {
            self.cacheModel.city = self.states[indexState].cities[indexCity].city;
            if (pickerView.numberOfComponents >= 3) {
                NSUInteger indexArea = [pickerView selectedRowInComponent:2];
                if (indexArea < self.states[indexState].cities[indexCity].areas.count) {
                    self.cacheModel.district = self.states[indexState].cities[indexCity].areas[indexArea];
                }
            }
        }
    }
    if (![self.cacheModel isEqual:self.areaModel]) {
        self.areaModel.state = self.cacheModel.state;
        self.areaModel.city = self.cacheModel.city;
        self.areaModel.district = self.cacheModel.district;
        [self sendActionsForControlEvents:UIControlEventValueChanged];
    }
}

#pragma mark - getter
- (ATAreaModel *)areaModel {
    if (!_areaModel) {
        _areaModel = [[ATAreaModel alloc] init];
    }
    return _areaModel;
}
- (ATAreaModel *)cacheModel {
    if (!_cacheModel) {
        _cacheModel = [[ATAreaModel alloc] init];
    }
    return _cacheModel;
}
- (UIPickerView *)picker {
    if (!_picker) {
        _picker = [[UIPickerView alloc] init];
        _picker.delegate = self;
        _picker.dataSource = self;
    }
    return _picker;
}
@end

@implementation ATAreaModel
- (BOOL)isEqual:(ATAreaModel *)object {
    return ([object isKindOfClass:[ATAreaModel class]])
    && ([self.state isEqualToString:object.state] || (!self.state && !object.state))
    && ([self.city isEqualToString:object.city] || (!self.city && !object.city))
    && ([self.district isEqualToString:object.district] || (!self.district && !object.district));
}
@end

@implementation ATStateResouce
+ (NSDictionary<NSString *,id> *)modelContainerPropertyGenericClass {
    return @{@"cities" : [ATCityResModel class]};
}
@end

@implementation ATCityResModel
@end


